package com.zgjkhis.modules.system.service.impl;

import com.github.pagehelper.PageInfo;
import com.zgjkhis.common.service.impl.BaseServiceImpl;
import com.zgjkhis.domain.PageResult;
import com.zgjkhis.dozer.service.IGenerator;
import com.zgjkhis.modules.system.domain.DrugWareHousingDetail;
import com.zgjkhis.modules.system.domain.vo.DrugWareHousingDetailVO;
import com.zgjkhis.modules.system.service.DrugWareHousingDetailService;
import com.zgjkhis.modules.system.service.dto.*;
import com.zgjkhis.modules.system.service.mapper.DrugWareHousingDetailMapper;
import com.zgjkhis.utils.FileUtil;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
 * @Author yzq
 * @Description 药品入库
 * @Date 2021/6/23 9:47
 * @Param
 * @return
 **/
@Service
//@CacheConfig(cacheNames = "user")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DrugWareHousingDetailServiceImpl extends BaseServiceImpl<DrugWareHousingDetailMapper, DrugWareHousingDetail> implements DrugWareHousingDetailService {

    @Resource
    private IGenerator generator;

    @Override
    public PageResult<DrugWareHousingDetailDto> findAllByPageable(DrugQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<DrugWareHousingDetailDto> page = new PageInfo<>(baseMapper.findAllByPageable(criteria));
        return PageResult.data(page);
    }

    @Override
    public PageResult<DrugWareHousingDetailDto> findAllWareDetailByPageable(DrugQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<DrugWareHousingDetailDto> page = new PageInfo<>(findAllWareDetail(criteria));
        return PageResult.data(page);
    }

    @Override
    public List<DrugWareHousingDetailDto> findAllWareDetail(DrugQueryCriteria criteria) {
        return baseMapper.findAllWareDetailByPageable(criteria);
    }

    @Override
    public PageResult<DrugRemainDto> findRemainByPage(DrugRemainQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<DrugRemainDto> page = new PageInfo<>(baseMapper.findRemainByPage(criteria));
        return PageResult.data(page);
    }

    @Override
    public List<DrugRemainDto> findRemainList(DrugRemainQueryCriteria criteria) {
        return baseMapper.findRemainByPage(criteria);
    }

    @Override
    public void dowonload(List<DrugRemainDto> drugRemainDtoList, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DrugRemainDto drugRemainDto : drugRemainDtoList) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("药品名称", drugRemainDto.getDrugName());
            map.put("库位号", drugRemainDto.getDrugNo());
            map.put("生产厂家", drugRemainDto.getDrugManufactor());
            map.put("规格", drugRemainDto.getDrugSpec());
            map.put("批号", drugRemainDto.getBatchNo());
            map.put("有效期", drugRemainDto.getTermOfValidity());
            map.put("剩余数量", drugRemainDto.getLeftCount()+drugRemainDto.getPackageUnit());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<DrugWareHousingDetailDto> findAll(DrugTypeQueryCriteria criteria) {
        return baseMapper.findAll(criteria);
    }

    @Override
    public List<DrugWareHousingDetailVO> queryList(String wareHousingNo) {
        return baseMapper.queryList(wareHousingNo);
    }

    @Override
    public List<DrugWareHousingDetailDto> getValidityDrugDetailList(ValidityDrugCriteria criteria) {
        getPage(PageRequest.of(criteria.getPage(), criteria.getSize(), Sort.by(Sort.Direction.ASC, "term_of_validity")));
        PageInfo<DrugWareHousingDetailDto> page = new PageInfo<>(baseMapper.getValidityDrugDetailList(criteria));
        return generator.convert(page.getList(), DrugWareHousingDetailDto.class);
    }

    @Override
    public void outDetails(DrugQueryCriteria criteria, HttpServletResponse response) throws IOException {

        List<DrugWareHousingDetailDto> drugWareHousingDetailDtos = baseMapper.findAllByPageable(criteria);


        List<Map<String, Object>> list = new ArrayList<>();
        for (DrugWareHousingDetailDto vo : drugWareHousingDetailDtos) {
            Map<String, Object> map = new LinkedHashMap<>();
            String type="";
            switch(Integer.valueOf(vo.getWareHousingType())) {
                case 1:
                    type="平台采购";
                    break;
                case 2:
                    type="退货入库";
                    break;
                case 3:
                    type="报溢";
                    break;
                case 4:
                    type="挑拨入库";
                    break;
                case 5:
                    type="赠品入库";
                    break;
                case 6:
                    type="普通入库";
                    break;
                case 7:
                    type="盘点入库";
                    break;
            }
            map.put("商品名称", vo.getDrugName());
            map.put("入库日期", vo.getWarehousingTime());
            map.put("供应商", vo.getSupplierName());
            map.put("进货价", vo.getCostPrice());
            map.put("入库数量", vo.getScheduledQuantity());
            map.put("入库方式", type);
            map.put("批次号", vo.getBatchNo());
            map.put("剩余数量", vo.getLeftCount());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<DrugWareHousingDetail> queryComfireList(Long id) {
        return baseMapper.queryComfireList(id);
    }
}
